package com.dgut.lotterySystem.service.activityStatus.operator;

import com.dgut.lotterySystem.dao.dataObject.ActivityDO;
import com.dgut.lotterySystem.dao.mapper.ActivityMapper;
import com.dgut.lotterySystem.dao.mapper.ActivityPrizeMapper;
import com.dgut.lotterySystem.service.dto.ConvertActivityStatusDTO;
import com.dgut.lotterySystem.service.enums.ActivityEnum;
import com.dgut.lotterySystem.service.enums.ActivityPrizeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        ActivityEnum activityEnum=convertActivityStatusDTO.getTargetActivityStatus();
        if(activityId==null||activityEnum==null) return false;

        ActivityDO activityDO= activityMapper.findActivityInfoById(activityId);
        if(activityDO==null) return false;

        if(activityDO.getStatus().equalsIgnoreCase(activityEnum.name())) return false;

        int count=activityPrizeMapper.InitCount(activityId, ActivityPrizeEnum.INIT.name());
        if(count>0) return false;
        //需要判断是否所有的奖品都为completed
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        ActivityEnum activityEnum=convertActivityStatusDTO.getTargetActivityStatus();
        try{
            activityMapper.updateStatus(activityId,activityEnum.name());
            return true;
        }catch (Exception r){
            return false;
        }
    }


}
